//
//  PhoneNumber_VC.m
//  SpendMoney
//
//  Created by  lixinxin on 2017/9/4.
//  Copyright © 2017年  lixinxin. All rights reserved.
//

#import "PhoneNumber_VC.h"
#import "UIButton+Style1.h"
#import "Login_VC.h"
#import "ForgotPassword_VC.h"

@interface PhoneNumber_VC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phont_TF;

@property (weak, nonatomic) IBOutlet UIButton *button;

@end

@implementation PhoneNumber_VC

#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = AppTitleStr;

    self.phont_TF.delegate = self;
    
    if ([cSysPlist objectForKey:User_Phone]) {
        self.phont_TF.text = [cSysPlist objectForKey:User_Phone];
    }
    [self.button setBackgroundColor:MAIN_COLOR];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - Action
- (IBAction)buttonAction:(UIButton *)sender {
    
    if (self.phont_TF.text.length == 0) {
        
        [self.phont_TF becomeFirstResponder];
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.phont_TF.text forKey:@"Mobile"];
    
    // 判断不要多次点击
    self.button.enabled = NO;
    [Network link:kCheckUser
       parameters:dic
        outOfTime:0.f
         callback:^(Network *response) {
             
             NSMutableDictionary *mDict = [NSMutableDictionary new];
             mDict[@""] = @"";
             mDict[@"UserNo"] = [cSysPlist objectForKey:User_ID];
             [Network link:kIsRealInformation parameters:mDict outOfTime:0.0 callback:^(Network *response) {
                 if (response.responCode == 1) {
                     NSString *appStr = response.dataDic[@"resultObj"][@"iOS"][@"appStore"];
                     [cSysPlist setObject:appStr forKey:@"appStr"];
                 }
             }];
             
             if (response.responCode==1) {
                 Login_VC *loginVC = [[Login_VC alloc] init];
                 loginVC.b = self.b;
                 loginVC.phoneNumber = self.phont_TF.text;
                 [self.navigationController pushViewController:loginVC animated:YES];
                 
             } else if(response.responCode==2) {
                 
                 ForgotPassword_VC *forgot = [[ForgotPassword_VC alloc] init];
                 forgot.title = @"注册";
                 forgot.tagStr = @"0";
                 forgot.phoneStr = self.phont_TF.text;
                 [self.navigationController pushViewController:forgot animated:YES];
             }
             self.button.enabled = YES;

         }];

}


#pragma mark - 字数限制
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (toBeString.length > 11) {
        return NO;
    } else {
    
        if (toBeString.length == 11) {
            [self.button addStyleMethod2];
        } else {
            [self.button addNotEnabledStyleMethod];
        }
    }

    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
